package com.ykm.serialportapp;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.ykm.serialport.JniUtil;
import com.ykm.serialport.SerialPortActivity;

public class MainActivity extends SerialPortActivity {
    private static final String TAG = "MainActivity";

    private String s = "you are greet!";
    private EditText tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        tv = (EditText) findViewById(R.id.sample_text);
        tv.setText(JniUtil.stringFromJNI());

        Log.i(TAG, "s: " + s);
        String encode = JniUtil.encode(s, s.length());
        Log.i(TAG, "encode: " + encode);
        String decode = JniUtil.decode(encode, encode.length());
        Log.i(TAG, "decode: " + decode);

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send:{
                Message message = Message.obtain();
                String s = tv.getText().toString();
                if (!s.isEmpty()){
                    message.obj = s.getBytes();
                    sendingHandler.sendMessage(message);
                }
            }
            break;
            default:
                break;
        }
    }

    @Override
    protected void onDataReceived(final byte[] buffer, final int size) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "onDataReceived: " + new String(buffer) + "  size = " + size);
            }
        });
    }
}
